package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BasePosition;
import com.ruoyi.base.service.IBasePositionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 岗位Controller
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@Api(description="岗位管理")
@RestController
@RequestMapping("/base/basePosition")
public class BasePositionController extends BaseController
{
    @Autowired
    private IBasePositionService basePositionService;

    /**
     * 查询岗位列表
     */
    @ApiOperation("查询岗位列表")
    @GetMapping("/list")
    public TableDataInfo list(BasePosition basePosition)
    {
        basePosition.setUnitCode(getLoginUser().getUser().getUnit());
        List<BasePosition> list = basePositionService.selectBasePositionList(basePosition);
        return getDataTable(list);
    }

    /**
     * 导出岗位列表
     */
    @ApiOperation("导出岗位列表")
    @Log(title = "岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasePosition basePosition)
    {
        List<BasePosition> list = basePositionService.selectBasePositionList(basePosition);
        ExcelUtil<BasePosition> util = new ExcelUtil<BasePosition>(BasePosition.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 获取岗位详细信息
     */
    @ApiOperation("获取岗位详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(basePositionService.selectBasePositionByID(ID));
    }

    /**
     * 新增岗位
     */
    @ApiOperation("新增岗位")
    @Log(title = "岗位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasePosition basePosition)
    {
        basePosition.setID(UUID.randomUUID().toString().replace("-", ""));
        basePosition.setCreatedBy(getUserId().toString());
        basePosition.setCreatedTime(new Date());
        basePosition.setUnitCode(getLoginUser().getUser().getUnit());
        return toAjax(basePositionService.insertBasePosition(basePosition));
    }

    /**
     * 修改岗位
     */
    @ApiOperation("修改岗位")
    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasePosition basePosition)
    {
        basePosition.setUpdatedBy(getUserId().toString());
        basePosition.setUpdatedTime(new Date());
        basePosition.setUnitCode(getLoginUser().getUser().getUnit());
        return toAjax(basePositionService.updateBasePosition(basePosition));
    }

    /**
     * 删除岗位
     */
    @ApiOperation("删除岗位")
    @Log(title = "岗位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(basePositionService.deleteBasePositionByIDs(IDs));
    }
}
